package com.litchi.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.SaveListener;

import com.litchi.R;
import com.litchi.bean.UserBean;
import com.litchi.utils.NetWorkUtils;
import com.litchi.utils.Utils;
import com.litchi.utils.Constants;

/**
 *  用户登录模块
 * @author Administrator
 *
 */
public class LoginActivity extends Activity implements OnClickListener, OnCheckedChangeListener{
	
	private ToggleButton mTgBtnShowPsw;
	private EditText mEditPsw;
	private EditText mEditUid;
	private TextView mFindPwd;
	private ImageView mBtnClearUid;
	private ImageView mBtnClearPsw;
	private Button mBtnLogin;
 	/**通过Bmob登录*/
	private final int LOG_BY_BMOB = 1;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		Bmob.initialize(this, "881230b0542b0d1540a1b9f77c01804a");
		initUI();
		setOnListener();
		initUid();
	}
	
	/**
	 * 初始化记住的用户名
	 */
	private void initUid() {
		SharedPreferences sp = getSharedPreferences(com.litchi.utils.Constants.USER_LOGIN_SP_KEY,Context.MODE_PRIVATE);
		String uid = sp.getString("uid", "");
		mEditUid.setText(uid);
	}
	private void setOnListener() {
		mEditUid.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (mEditUid.getText().toString().length() > 0) {
					mBtnClearUid.setVisibility(View.VISIBLE);
					if (mEditPsw.getText().toString().length() >0) {
						mBtnLogin.setEnabled(true);
					} else {
						mBtnLogin.setEnabled(false);
					}
				} else {
					mBtnLogin.setEnabled(false);
					mBtnClearUid.setVisibility(View.INVISIBLE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		mEditPsw.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (mEditPsw.getText().toString().length() > 0) {
					mBtnClearPsw.setVisibility(View.VISIBLE);
					if (mEditUid.getText().toString().length() > 0) {
						mBtnLogin.setEnabled(true);
					} else {
						mBtnLogin.setEnabled(false);
					}
				} else {
					mBtnLogin.setEnabled(false);
					mBtnClearPsw.setVisibility(View.INVISIBLE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		mBtnLogin.setOnClickListener(this);
		mBtnClearUid.setOnClickListener(this);
		mBtnClearPsw.setOnClickListener(this);
		mTgBtnShowPsw.setOnCheckedChangeListener(this);
		mFindPwd.setOnClickListener(this);
		findViewById(R.id.img_back).setOnClickListener(this);
		 
		findViewById(R.id.tv_quick_sign_up).setOnClickListener(this);
	}

	private void initUI() {
		mBtnLogin = (Button) findViewById(R.id.btn_login);
		mEditUid = (EditText) findViewById(R.id.edit_uid);
		mEditPsw = (EditText) findViewById(R.id.edit_psw);
		mBtnClearUid = (ImageView) findViewById(R.id.img_login_clear_uid);
		mBtnClearPsw = (ImageView) findViewById(R.id.img_login_clear_psw);
		mTgBtnShowPsw = (ToggleButton) findViewById(R.id.tgbtn_show_psw);
		mFindPwd=(TextView)findViewById(R.id.tv_find_back_psw);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:	//登录
			
			if (NetWorkUtils.isNetworkAvailabel(this)) {
				login();
			}else {
				Utils.showToast(this,"请检查网络是否可用");
			}
			
			break;
		case R.id.img_back:	//返回
			finish();
			break;
		 
		case R.id.img_login_clear_uid:	//清除用户名
			clearText(mEditUid);
			break;
		case R.id.img_login_clear_psw:	//清除密码
			clearText(mEditPsw);
			break;
		case R.id.tv_quick_sign_up:	//快速注册
			startActivity(new Intent(this, SignUpActivity.class));
			break;
		case R.id.tv_find_back_psw:
			startActivity(new Intent(this,FindPwdActivity.class));
		default:
			break;
		}
	}
	
	 
    
	/**
	 * 清空控件文本
	 * @param mEditUid2
	 */
	private void clearText(EditText edit) {
		edit.setText("");
	}
	

	/**
	 * 登录按钮触发
	 */
	private void login() {
		String userName = mEditUid.getText().toString();
		String pwd = mEditPsw.getText().toString();

		final UserBean user = new UserBean();
		user.setUsername(userName);
		user.setPassword(pwd);
		user.login(this, new SaveListener() {
			public void onSuccess() {
				//将用户名保存到Preference
				SharedPreferences sp = getSharedPreferences("login",Context.MODE_PRIVATE);
				Editor edit = sp.edit();
				edit.putString(Constants.USER_ID_KEY, mEditUid.getText().toString());
				edit.putString(com.litchi.utils.Constants.USER_PWD_KEY, mEditPsw.getText().toString());
				edit.putString(com.litchi.utils.Constants.USER_KEY, user.getObjectId());
				edit.commit();
				
				Toast.makeText(LoginActivity.this, "登录成功",
						Toast.LENGTH_SHORT).show();
				 
				Intent intent = new Intent();
				intent.putExtra(Constants.USER_ID_KEY, mEditUid.getText().toString());
				setResult(com.litchi.utils.Constants.INTENT_KEY.LOGIN_RESULT_SUCCESS_CODE, intent );
				LoginActivity.this.finish();
			}

			public void onFailure(int arg0, String arg1) {
				Toast.makeText(LoginActivity.this, "登录失败，"+arg1, Toast.LENGTH_LONG)
						.show();
			}
		});
	}

	/**
	 * 是否显示密码
	 */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			//显示密码
			mEditPsw.setTransformationMethod(
					HideReturnsTransformationMethod.getInstance());
		} else {
			//隐藏密码
			mEditPsw.setTransformationMethod(
					PasswordTransformationMethod.getInstance());
		}
	}

}
